package com.canbot.u05.activity.agenda

import android.content.Intent
import android.view.View
import android.widget.TextView
import com.canbot.u05.R
import com.canbot.u05.activity.agenda.bean.AgendaWorkDayBean
import com.canbot.u05.activity.presidemode.newcode.BaseHostActivity

/**
 * @author yinxiaowei
 * @date 2019/5/31
 */
class AgendaWorkDayActivity : BaseHostActivity(), View.OnClickListener {
    private lateinit var ivOne: View
    private lateinit var ivMore: View
    private var mAgendaWorkDayBean: AgendaWorkDayBean = AgendaWorkDayBean()

    override fun initEventData() {
        val workDayOne = findViewById<View>(R.id.work_day_one)
        workDayOne.setOnClickListener(this)
        val textViewOne = workDayOne.findViewById<TextView>(R.id.type_name)
        textViewOne.text = getString(R.string.work_one)
        ivOne = workDayOne.findViewById(R.id.btn_isSeletor)
        val workDayMore = findViewById<View>(R.id.work_day_more)

        val textViewMore = workDayMore.findViewById<TextView>(R.id.type_name)
        textViewMore.text = getString(R.string.work_more)
        ivMore = workDayMore.findViewById(R.id.btn_isSeletor)
        workDayMore.setOnClickListener(this)
        initIntent()
    }

    private fun initIntent() {
        mAgendaWorkDayBean = intent.getSerializableExtra(DATA) as AgendaWorkDayBean
        if (mAgendaWorkDayBean.dayOne) {
            ivMore.visibility = View.INVISIBLE
            ivOne.visibility = View.VISIBLE
        } else {
            ivMore.visibility = View.VISIBLE
            ivOne.visibility = View.INVISIBLE
        }
    }

    override fun getLayoutResId(): Int {
        return R.layout.activity_agenda_work_day
    }

    private fun exit() {
        val intent = Intent()
        intent.putExtra(BaseHostActivity.DATA, mAgendaWorkDayBean)
        setResult(workType, intent)
        finish()
    }

    override fun onBackPressed() {
        exit()
    }

    fun back(view: View) {
        exit()
    }

    private val TAG = AgendaWorkDayActivity::class.java.simpleName
    private var workType: Int = AgendaWorkChoiceActivity.WORK_ONE
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        when (requestCode) {
            REQUEST_WORK_DAY_CHOICE -> {
                workType = resultCode
                val agendaWorkDayBean: AgendaWorkDayBean = data?.getSerializableExtra(DATA) as AgendaWorkDayBean
                val list = agendaWorkDayBean.data
                if (list.size > 0) {
                    mAgendaWorkDayBean.data.clear()
                    mAgendaWorkDayBean.data.addAll(list)
                    exit()
                } else {

                }
            }
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.work_day_one -> {
                ivMore.visibility = View.INVISIBLE
                ivOne.visibility = View.VISIBLE
                if (!mAgendaWorkDayBean.dayOne) {
                    mAgendaWorkDayBean.dayOne = true
                    mAgendaWorkDayBean.data.clear()
                }
                gotoSelectActivity(AgendaWorkChoiceActivity.WORK_ONE)
            }
            R.id.work_day_more -> {
                ivOne.visibility = View.INVISIBLE
                ivMore.visibility = View.VISIBLE
                if (mAgendaWorkDayBean.dayOne) {
                    mAgendaWorkDayBean.dayOne = false
                    mAgendaWorkDayBean.data.clear()
                }
                gotoSelectActivity(AgendaWorkChoiceActivity.WORK_MORE)
            }
        }
    }

    private fun gotoSelectActivity(type: Int) {
        val intent = Intent()
        intent.setClass(this, AgendaWorkChoiceActivity::class.java)
        intent.putExtra(DATA, mAgendaWorkDayBean)
        gotoActivityForResult(intent, REQUEST_WORK_DAY_CHOICE)
    }
}
